%For fixed q, calculate second-best solution for loan portfolio case


iGridB=1000;
aBGrid=linspace(0,aBar,iGridB);
qB=q;

for i=1:iGridB
    
    aBPre=aBGrid(i);
    
    W=aBPre*phi;
    FBPre=2-(gamma-r)*W*Agency-phi*aBPre^2+2*(Lambda-aBPre-qB)*FB;
    FBPre=FBPre/(r+2*(Lambda-aBPre-qB));
    vFBPre(i)=FBPre;
    
end

[FBPre,ind]=max(vFBPre);
aBPre=aBGrid(ind);
WBPre=phi*aBPre;
lambdaBPre=Lambda-aBPre-qB;

cBPre=(gamma+2*lambdaB)*WBPre+phi*aBPre^2;
VBPre=(WBPre+lambdaBPre*VB)/(gamma+2*lambdaBPre);

